#include "tracker.h"

tracker::tracker(QObject *parent) :
    QObject(parent)
{
    timer=new QTimer(this);
    QObject:: connect( timer, SIGNAL(timeout()), SLOT(time_done()) );
    secs=0;
    vector.resize(5);
    QDir dir(IODATA_RECORD_DIR);
    if(!dir.exists()) dir.mkpath(IODATA_RECORD_DIR);
}

tracker::~tracker()
{
    delete timer;
}

void tracker::start_track()
{
    startTime=QDateTime::currentDateTime();
    timer->start(1000);
}

void tracker::stop_track()
{
    timer->stop();
    writeto( IODATA_RECORD_DIR + controller->rec->starttime,*track);
    track_data_list.clear();
}

void tracker::time_done()
{

    read_IO();

    vector[0]=++secs;
    vector[1]=PSU;
    vector[2]=(int)(TEM*10);
    vector[4]=sterilization_stage;

 if( IObuffer==IOUN.integer_value )
 {
     vector[3]=0;
     if(multi_jump)  {if(track_data_list.size()) track_data_list.last()[0]++;}
     else {multi_jump=true;track_data_list << vector;}
 }
 else
 {//没有预先为Qvector分配空间，直接用[]访问出错
     if(multi_jump) multi_jump=false;
     vector[3]=IOUN.integer_value;
     IObuffer=IOUN.integer_value;
     track_data_list << vector;
 }
 //qDebug() <<QString::number(track_data_list.size());

}


QDataStream &operator<<(QDataStream &datastream, const tracker &data)
{
    return datastream
    << data.startTime
    << data.track_data_list;

}
QDataStream &operator>>(QDataStream &datastream, tracker &data)
{
    datastream >> data.startTime
               >> data.track_data_list;
      return datastream;
}

void readfrom(QString strTemp,tracker &track) //strTemp 读文件名
  {
      QFile fileRead(strTemp);
      fileRead.open(QIODevice::ReadOnly);
      QDataStream  readDataStream(&fileRead);
      readDataStream >> track;
      fileRead.close();
   }

void writeto(QString strTemp,tracker &track)
  {
      QFile fileRead(strTemp);
      fileRead.open(QIODevice::WriteOnly);
      QDataStream  writeDataStream(&fileRead);
      writeDataStream << track;
      fileRead.close();
   }


void tracker::read_IO()
{


IOUN.iobs.bottom_water_level=bottom_water_level_isreached ;
IOUN.iobs.top_water_level=top_water_level_isreached;
IOUN.iobs.colling_water_switch=water_switch_isopen;
IOUN.iobs.inter_pressure_switch=inter_pressure_switch_isreached;
IOUN.iobs.front_door_closed_in_place_switch=front_door_closed_in_place;
IOUN.iobs.front_door_opened_in_place_switch=front_door_opened_in_place;
IOUN.iobs.front_door_Sealing_strip_pressure=front_door_Sealing_strip_pressure_in_place;
IOUN.iobs.back_door_opened_in_place_switch=back_door_opened_in_place;
IOUN.iobs.back_door_closed_in_place_switch=back_door_closed_in_place;
IOUN.iobs.back_door_Sealing_strip_pressure_in_place_switch=back_door_Sealing_strip_pressure_in_place;
//IOUN.iobs.overpressure_protection_switch_isreached_switch=overpressure_protection_switch_isreached;

IOUN.iobs.water_valve=water_valve_isopen;
IOUN.iobs.heating_valve=heating_valve_isopen;
IOUN.iobs.air_intake_valve=air_intake_valve_isopen;
IOUN.iobs.exhaust_valve=exhaust_valve_isopen;
IOUN.iobs.Vacuum_pump=Vacuum_pump_isopen;
IOUN.iobs.air_supply_valve=air_supply_valve_isopen;
IOUN.iobs.open_front_door_valve=READ_GPIO_PIN(GPMDAT,2);
IOUN.iobs.close_front_door_valve=READ_GPIO_PIN(GPMDAT,3);
IOUN.iobs.front_door_Sealing_strip_Inflation_valve=READ_GPIO_PIN(GPMDAT,4);
IOUN.iobs.front_door_Sealing_strip_Exhaust_valve=READ_GPIO_PIN(GPMDAT,5);
IOUN.iobs.open_back_door_valve=READ_GPIO_PIN(GPQDAT,1);
IOUN.iobs.close_back_door_valve=READ_GPIO_PIN(GPQDAT,2);
IOUN.iobs.back_door_Sealing_strip_Inflation_valve=READ_GPIO_PIN(GPEDAT,4);
IOUN.iobs.back_door_Sealing_strip_Exhaust_valve=READ_GPIO_PIN(GPQDAT,4);
//IOUN.iobs.status=sterilization_stage;
}
